/*
 * StatusView.java
 *
 * Created on 12 Август 2010 г., 21:26
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package protocol;

import DrawControls.icons.Icon;
import DrawControls.text.Parser;
import jimm.comm.StringConvertor;
import jimm.comm.Util;
import jimm.ui.*;
import jimm.ui.menu.*;
import jimm.util.JLocale; //status info
import protocol.icq.*;
import protocol.jabber.*;

/**
 *
 * @author Vladimir Kryukov
 */
public class StatusView extends TextListEx implements SelectListener {
    public static final int INFO_MENU_COPY     = 1;
    public static final int INFO_MENU_COPY_ALL = 2;
    public static final int INFO_MENU_GOTO_URL = 4;
    public static final int INFO_MENU_GET_X    = 5;
    private Protocol protocol;
    private Contact contact;
    // #sijapp cond.if modules_CLIENTS is "true" #
    private String clientVersion;
    private String clientOS; //status info
    // #sijapp cond.end #


    /** Creates a new instance of StatusView */
    public StatusView() {
        super(null);
    }
    public void select(Select select, MenuModel model, int cmd) {
        switch (cmd) {
            case INFO_MENU_COPY:
            case INFO_MENU_COPY_ALL:
                copy(INFO_MENU_COPY_ALL == cmd);
                restore();
                break;

            case MenuModel.BACK_COMMAND_CODE:
                back();
                clear();
                break;

            case INFO_MENU_GOTO_URL:
                new TextListEx("").gotoURL(getCurrText());
                break;

                // #sijapp cond.if protocols_ICQ is "true" #
                // #sijapp cond.if modules_XSTATUSES is "true" #
            case INFO_MENU_GET_X:
                ((Icq)protocol).requestXStatusMessage(contact);
                restore();
                break;
                // #sijapp cond.end #
                // #sijapp cond.end #
        }
    }
    
    private void addBoldText(String text, byte colorType) {           //add
        getDefaultParser().addText(text, colorType, FONT_STYLE_BOLD); //add
        getDefaultParser().commit();                                  //add
    }                                                                 //add    

    // #sijapp cond.if modules_CLIENTS is "true" #
    public void addClient(Icon icon, String name) {
        if (!"".equals(clientVersion) && clientVersion != null) { //status info
        addBoldText(JLocale.getString("dc_info"), THEME_TEXT);    //status info
        addPlain(icon, clientVersion);                            //status info
        } else if (name != null) {                                //status info
        addBoldText(JLocale.getString("dc_info"), THEME_TEXT);    //status info
        addPlain(icon, name);                                     //status info
        }                                                         //status info
        if (!"".equals(clientOS) && clientOS != null) {           //status info
        addBoldText(JLocale.getString("OS"), THEME_TEXT);         //status info
        addPlain(null, clientOS);                                 //status info
        }                                                         //status info
    }
    public void setClientVersion(String version) {
        clientVersion = version;
    }
    public void setOSVersion(String os) { //status info
        clientOS = os;                    //status info
    }                                     //status info
    // #sijapp cond.end #

    public void addTime(Contact c) {
        if (!contact.isSingleUserContact()) {
            return;
        }
        // #sijapp cond.if protocols_JABBER is "true" #
        if (contact instanceof JabberServiceContact) {
            return;
        }
        // #sijapp cond.end #
        long signonTime = contact.chaingingStatusTime;
        if (0 < signonTime) {
            long now = Util.createCurrentDate(true);
            long localTime = Util.gmtTimeToLocalTime(signonTime);
            if (c.isOnline()) {
                addInfo("li_signon_time", Util.getDateString(localTime, false));
                addInfo("li_online_time", Util.longitudeToString(now - signonTime));
            } else {
                addInfo("li_signoff_time", Util.getDateString(localTime, false));
            }
        }
    }
    /////////////////////

    public void addPlain(Icon img, String str) {
        if (!StringConvertor.isEmpty(str)) {
            int id = uiBigTextIndex++;
            Parser line = createParser(id);
            if (null != img) {
                line.addImage(img);
            }
            line.addText(str, THEME_TEXT, FONT_STYLE_PLAIN);
            addPar(line);
        }
    }
    public void addStatusText(String text) {
        if (!StringConvertor.isEmpty(text)) {
            int id = uiBigTextIndex++;
            Parser line = createParser(id);
            line.addText(text, THEME_PARAM_VALUE, FONT_STYLE_PLAIN);
            addPar(line);
        }
    }
    public void addInfo(String key, String value) {
        addParam(key, value);
    }
    /////////////////////
    public void addContactStatus() {
        Parser line = createParser(-1);                                                //status info
        line.addText(JLocale.getString("status") + "\n", THEME_TEXT, FONT_STYLE_BOLD); //status info
        addPar(line);                                                                  //status info
        byte status = contact.getStatusIndex();
        StatusInfo info = protocol.getStatusInfo();
        addStatus(info.getIcon(status), info.getName(status));
    }
    // #sijapp cond.if modules_XSTATUSES is "true" #
    public void addXStatus() {
        XStatusInfo info = protocol.getXStatusInfo();
        int x = contact.getXStatusIndex();
        addStatus(info.getIcon(x), info.getName(x));
    }
    // #sijapp cond.end #
    public void addStatus(Icon icon, String name) {
        addPlain(icon, name);
    }
    protected MenuModel getMenu() {
        MenuModel menu = new MenuModel();
        menu.addItem("copy_text",     INFO_MENU_COPY);
        menu.addItem("copy_all_text", INFO_MENU_COPY_ALL);
        if ((1 < getCurrTextIndex()) && Util.hasURL(getCurrText())) {
            menu.addItem("goto_url", INFO_MENU_GOTO_URL);
        }
        // #sijapp cond.if protocols_ICQ is "true" #
        // #sijapp cond.if modules_XSTATUSES is "true" #
        if ((XStatusInfo.XSTATUS_NONE != contact.getXStatusIndex())
                && (protocol instanceof Icq)) {
            menu.addItem("reqxstatmsg", INFO_MENU_GET_X);
        }
        // #sijapp cond.end #
        // #sijapp cond.end #
        menu.setActionListener(this);
        return menu;
    }
    public void init(Protocol p, Contact c) {
        contact = c;
        protocol = p;
        // #sijapp cond.if modules_CLIENTS is "true" #
        clientVersion = null;
        clientOS = null; //status info
        // #sijapp cond.end #
    }
    public void initUI() {
        setDefaultCode(INFO_MENU_COPY);
        lock();
        clear();
        setCaption(contact.getName());
        Parser line = createParser(-1);                                                   //status info
        line.addText(JLocale.getString("main_info") + "\n", THEME_TEXT, FONT_STYLE_BOLD); //status info
        addPar(line);                                                                     //status info
        addInfo(protocol.getUserIdName(), contact.getUserId());
    }
    public Contact getContact() {
        return contact;
    }
    public void showIt() {
        unlock();
        super.show();
    }
}
